﻿package idv.cjcat.emitter {
	use namespace engine;
	
	import idv.cjcat.emitter.geom.*;
	
	/**
	 * Rectangle-shaped particle source.
	 */
	public class RectSource extends ParticleSource {
		
		public var width:Number;
		public var height:Number;
		public var normal:Vector2D = new Vector2D(0, -1);
		public var centerAligned:Boolean = false;
		/**
		 * The higher the center-bias iteration, the more particles are concentrated to the center of the source.
		 */
		public var centerBiasIterations:uint = 0;
		
		/**
		 * Rectangular-shaped particle source.
		 * @param	x
		 * @param	y
		 * @param	width
		 * @param	height
		 */
		public function RectSource(x:Number = 0, y:Number = 0, width:Number = 140, height:Number = 70) {
			super(x, y);
			this.width = width;
			this.height = height;
		}
		
		override public function spawnPos():MotionData2D {
			var temp:MotionData2D = new MotionData2D();
			var slopeAng:Number = Math.atan2(normal.y, normal.x) + Math.PI / 2;
			var randPos:Number = EmitterMath.normalDistribution(0.5, 0.5, centerBiasIterations + 1);
			var randPos2:Number = EmitterMath.normalDistribution(0.5, 0.5, centerBiasIterations + 1);
			if (centerAligned) {
				temp.x = x + EmitterMath.interpolate(0, -width * 0.5 * Math.cos(slopeAng), 1, width * 0.5 * Math.cos(slopeAng), randPos);
				temp.y = y + EmitterMath.interpolate(0, -width * 0.5 * Math.sin(slopeAng), 1, width * 0.5 * Math.sin(slopeAng), randPos);
				temp.x -= EmitterMath.interpolate(0, -height * 0.5 * Math.sin(slopeAng), 1, height * 0.5 * Math.sin(slopeAng), randPos2);
				temp.y += EmitterMath.interpolate(0, -height * 0.5 * Math.cos(slopeAng), 1, height * 0.5 * Math.cos(slopeAng), randPos2);
				return temp;
			} else {
				temp.x = x + EmitterMath.interpolate(0, 0, 1, width *  Math.cos(slopeAng), randPos);
				temp.y = y + EmitterMath.interpolate(0, 0, 1, width * Math.sin(slopeAng), randPos);
				temp.x -= EmitterMath.interpolate(0, 0, 1, height * Math.sin(slopeAng), randPos2);
				temp.y += EmitterMath.interpolate(0, 0, 1, height * Math.cos(slopeAng), randPos2);
				return temp;
			}
		}
		
		
		//XML
		//------------------------------------------------------------------------------------------------
		
		override public function toXML():XML {
			var xml:XML = super.toXML();
			xml.@type = "rect";
			xml.@width = width.toString();
			xml.@height = height.toString();
			xml.@normalX = normal.x.toString();
			xml.@normalY = normal.y.toString();
			xml.@centerAligned = centerAligned.toString();
			xml.@centerBiasIterations = centerBiasIterations.toString();
			
			return xml;
		}
		override public function parseXML(xml:XML):void {
			super.parseXML(xml);
			
			width = parseFloat(xml.@width);
			height = parseFloat(xml.@height);
			normal.x = parseFloat(xml.@normalX);
			normal.y = parseFloat(xml.@normalY);
			centerAligned = (xml.@centerAligned == "true");
			centerBiasIterations = parseInt(xml.@centerBiasIterations);
		}
		
		//------------------------------------------------------------------------------------------------
		//end of XML
	}
}